package com.example.baseapp.adpter

import android.view.View
import androidx.recyclerview.widget.LinearLayoutManager
import androidx.recyclerview.widget.RecyclerView
import com.chad.library.adapter.base.BaseSectionQuickAdapter
import com.chad.library.adapter.base.BaseViewHolder
import com.example.baseapp.R
import com.example.baseapp.entry.MyFirstSection
import com.example.baseapp.entry.MySecondSection

/**
 * <pre>
 *
 *     @author  : renhailong
 *     @since   : 2021/2/22 0022 14:24
 *     @version : 1.0
 * </pre>
 */
class FirstLayerAdapter(data: MutableList<MyFirstSection>) :
    BaseSectionQuickAdapter<MyFirstSection, BaseViewHolder>(
        R.layout.item_layout,
        R.layout.header_layout,
        data
    ) {

    var currentExpandPosition: Int = -1

    fun changeExpandItem(position:Int){
        //大于默认值，说明有展开的item
        if(currentExpandPosition > -1){
            //修改状态并折叠
            getItem(currentExpandPosition)!!.isExpand = false
            notifyItemChanged(currentExpandPosition)
        }
        //点击的position和本次记录的position对比，是否同一次位置
        //如果不是
        if(position != currentExpandPosition) {
            //把点击的item展开
            getItem(position)!!.isExpand = true
            notifyItemChanged(position)
            //把位置信息更新
            currentExpandPosition = position
        }else{
            //如果是同一次位置，因为item已经折叠了，只需把状态恢复为默认的
            currentExpandPosition = -1
        }

    }

    override fun convert(helper: BaseViewHolder?, item: MyFirstSection?) {

        val secondSectionList = mutableListOf<MySecondSection>()
        helper?.let {
            it.setText(R.id.tv_classHourName, "${item?.classHourName}(可收缩)")
                .addOnClickListener(R.id.tv_classHourName)

            val recyclerView = helper.getView<RecyclerView>(R.id.rv_secondRecycler)
            recyclerView.layoutManager = LinearLayoutManager(mContext)
            
            if (item?.t?.syncClass != null) {
                val headerSection = MySecondSection(true, "同步课")
                secondSectionList.add(headerSection)
                for (syncClass in item.t.syncClass) {
                    val itemSection = MySecondSection(syncClass)
                    secondSectionList.add(itemSection)
                }
            }

            if (item?.t?.goodClass != null) {
                val headerSection = MySecondSection(true, "培优课")
                secondSectionList.add(headerSection)
                for (goodClass in item.t.goodClass) {
                    val itemSection = MySecondSection(goodClass)
                    secondSectionList.add(itemSection)
                }
            }

            recyclerView.adapter = SecondLayerAdapter(secondSectionList)

            if (item?.isExpand == true){
                recyclerView.visibility = View.VISIBLE
            }else{
                recyclerView.visibility = View.GONE
            }
        }
    }


    override fun convertHead(helper: BaseViewHolder?, item: MyFirstSection?) {
        helper?.setText(R.id.tv_headSection, item?.header)
    }

}